﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OleDb;
using Aandelendelen.Db;
using Aandelendelen.Obs;

namespace Aandelendelen.Controls
{
	public partial class MyCreditsControl : AuthenticationRequiredBaseUserControl
	{
		bool export = false;

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.SetSaldo();

			this.SetTotals();

			this.SetSaldoDetails();
		}

		private void SetSaldo()
		{
			this.lblSaldo.Amount = this.UserInfo.Saldo;
		}

		private void SetTotals()
		{
			UserTotals totals = new UserTotals(this.UserInfo.Id);

			this.GridViewTotals.DataSource = totals;

			this.GridViewTotals.DataBind();
		}

		private void SetSaldoDetails()
		{
			SaldoDetails details = new SaldoDetails(this.UserInfo.Id);

			this.GridView1.Columns[1].FooterText = ObjectFormatter.GetCurrencyString(details.TotalCredit);
			this.GridView1.Columns[2].FooterText = ObjectFormatter.GetCurrencyString(details.TotalDebit);

			this.GridView1.DataSource = details;

			this.GridView1.DataBind();
		}

		protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
		{
			this.GridView1.PageIndex = e.NewPageIndex;

			this.SetSaldoDetails();
		}

		protected override void Render(HtmlTextWriter writer)
		{
			if (!export)
				base.Render(writer);
			else
			{
				try
				{
					if (this.RadioButtonList1.SelectedValue == "Excel")
						this.RenderToExcel();
					else if (this.RadioButtonList1.SelectedValue == "Csv")
						this.RenderToCsv();
					else if (this.RadioButtonList1.SelectedValue == "Xml")
						this.RenderToXml();
					else
						base.Render(writer);
				}
				finally
				{
					this.export = false;
				}
			}
		}

		private void RenderToExcel()
		{
			string attachment = "attachment; filename=AandelenDelen.xls";

			Response.ClearContent();

			Response.AddHeader("content-disposition", attachment);

			Response.ContentType = "application/ms-excel";

			StringWriter stw = new StringWriter();

			HtmlTextWriter htextw = new HtmlTextWriter(stw);

			bool allowPaging = this.GridView1.AllowPaging;

			this.GridView1.AllowPaging = false;
			this.SetSaldoDetails();

			this.GridView1.RenderControl(htextw);

			this.GridView1.AllowPaging = allowPaging;
			this.SetSaldoDetails();

			Response.Write(stw.ToString());

			Response.End();
		}

		private void RenderToCsv()
		{
			string attachment = "attachment; filename=AandelenDelen.txt";

			Response.ClearContent();

			Response.AddHeader("content-disposition", attachment);

			Response.ContentType = "text/txt";

			StringWriter stw = new StringWriter();

			SaldoDetails details = new SaldoDetails(this.UserInfo.Id);

			details.SaveAsCSV(stw);

			Response.Write(stw.ToString());

			Response.End();
		}

		private void RenderToXml()
		{
			string attachment = "attachment; filename=AandelenDelen.xml";

			Response.ClearContent();

			Response.AddHeader("content-disposition", attachment);

			Response.ContentType = "text/xml";

			StringWriter stw = new StringWriter();

			SaldoDetails details = new SaldoDetails(this.UserInfo.Id);

			details.WriteXml(stw);

			Response.Write(stw.ToString());

			Response.End();
		}

		protected void Button2_Click(object sender, EventArgs e)
		{
			this.export = true;
		}
	}
}